package cn.ibizlab.codegen.groovy

import groovy.text.Template
import net.ibizsys.codegen.groovy.engine.GroovyCodeGenEngineBase
import net.ibizsys.model.IPSModelObject
import net.ibizsys.model.IPSObject
import net.ibizsys.model.IPSSystem
import net.ibizsys.model.dataentity.IPSDataEntity
import org.springframework.util.ObjectUtils

class GroovyCodeGenEngine extends GroovyCodeGenEngineBase {

    @Override
    protected void onInit() throws Exception {
        super.onInit()
    }

    @Override
    String getContent(IPSObject iPSObject, String strTemplateId) throws Exception {

        Template template = this.getTemplate(strTemplateId);

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("item", iPSObject);
        IPSSystem iPSSystem = null;

        if (iPSObject instanceof IPSSystem) {
            iPSSystem = (IPSSystem) iPSObject;
        } else {
            if (iPSObject instanceof IPSModelObject) {
                iPSSystem = ((IPSModelObject) iPSObject).getParentPSModelObject(IPSSystem.class, false);
            }
        }
        params.put("pub", iPSSystem.getDefaultPSSysSFPub());
        params.put("sys", iPSSystem);
        params.put("ctx", this.getContext());

        StringWriter sw = new StringWriter();
        template.make(params).writeTo(sw);

        return sw.toString();
    }

    @Override
    protected void onOutput(IPSSystem iPSSystem, String outputPath, Object[] filters, Map<String, IPSModelObject> fileMap, boolean bIgnoreException) throws Exception {
        if (iPSSystem.getDefaultPSSysSFPub() == null) {
            throw new Exception("未定义系统默认发布对象")
        }

        if (ObjectUtils.isEmpty(iPSSystem.getDefaultPSSysSFPub().getPKGCodeName())) {
            throw new Exception("系统默认发布对象未定义包代码标识")
        }

        if (ObjectUtils.isEmpty(iPSSystem.getDefaultPSSysSFPub().getCodeName())) {
            throw new Exception("系统默认发布对象未定义代码标识")
        }

        String outputRootPath = outputPath
        if (true) {
            //pom
//            output(iPSSystem, "/pom.xml.tpl", outputRootPath + "/pom.xml", fileMap, bIgnoreException)
//            output(iPSSystem, "/.gitignore", outputRootPath + "/.gitignore", fileMap, bIgnoreException)
            //core pom
//            output(iPSSystem, "/core/pom.xml.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/pom.xml", fileMap, bIgnoreException)
            output(iPSSystem, "/core/groovy/util/RTCodeUtils.java.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-util/src/main/java/net/ibizsys/central/cloud/core/util/RTCodeUtils.java", fileMap, bIgnoreException)
            //provider pom
//            output(iPSSystem, "/provider/pom.xml.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-provider/pom.xml", fileMap, bIgnoreException)

            //core 全局基类

            //provider 启动类
            output(iPSSystem, "/provider/java/RuntimeApplication.java.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-provider/src/main/java/" + iPSSystem.getPKGCodeName().replace(".", "/") + "/RuntimeApplication.java", fileMap, bIgnoreException)
            output(iPSSystem, "/provider/resources/logback-spring.xml.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-provider/src/main/resources/logback-spring.xml", fileMap, bIgnoreException)
            output(iPSSystem, "/provider/resources/servicehub.yml.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-provider/src/main/resources/servicehub.yml", fileMap, bIgnoreException)
//            output(iPSSystem, "/provider/resources/application.yml.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-provider/src/main/resources/application.yml", fileMap, bIgnoreException)
//            output(iPSSystem, "/provider/resources/application-sys.yml.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-provider/src/main/resources/application-sys.yml", fileMap, bIgnoreException)

        }

        List<IPSDataEntity> psDataEntityList = iPSSystem.getAllPSDataEntities()
        if (!ObjectUtils.isEmpty(psDataEntityList)) {
            psDataEntityList.each { entity ->
                if (testOutput(entity, filters) ) {
                    // doc
//                    output(entity, "/doc/DataEntity.md.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/doc/" + entity.getPKGCodeName().replace(".", "/") + "/dataentity/" + entity.realCodeName + ".md", fileMap, bIgnoreException)

                    // dto
                    entity.getAllPSDEMethodDTOs().each { dto ->
                        if (testOutput(dto, filters)) {
                            output(dto, "/core/groovy/entity/dto.groovy.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/src/main/groovy/" + entity.getPKGCodeName().replace(".", "/") + "/" + entity.realCodeName.toLowerCase() + "/dto/" + dto.realCodeName + ".groovy", fileMap, bIgnoreException)
                        }
                    }

                    // runtime
                    output(entity, "/core/groovy/entity/runtime.groovy.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/src/main/groovy/" + entity.getPKGCodeName().replace(".", "/") + "/" + entity.realCodeName.toLowerCase() + "/"  + entity.realCodeName + ".groovy", fileMap, bIgnoreException)

                    // logic
                    entity.getAllPSDELogics()?.each { logic ->
                        if (logic.isEnableBackend()) {
                            output(logic, "/core/groovy/entity/logic.groovy.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/src/main/groovy/" + entity.getPKGCodeName().replace(".", "/") + "/" + entity.realCodeName.toLowerCase() + "/logic/" + logic.realCodeName.capitalize() + ".groovy", fileMap, bIgnoreException)
                        }
                    }

                    // service
//                    output(entity, "/core/groovy/entity/service.groovy.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/src/main/groovy/" + entity.getPKGCodeName().replace(".", "/") + "/service/I" + entity.realCodeName + "Service.groovy", fileMap, bIgnoreException)

                    // security
//                    output(entity, "/core/groovy/entity/security.groovy.tpl", outputRootPath + "/" + iPSSystem.getDefaultPSSysSFPub().realCodeName.toLowerCase() + "-core/src/main/groovy/" + entity.getPKGCodeName().replace(".", "/") + "/" + entity.realCodeName.toLowerCase() + "/" +  entity.realCodeName + "AccessManager.groovy", fileMap, bIgnoreException)

                }
            }
        }
    }


}
